package com.online.fund.spider;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.online.fund.spider.dao.FundDao;
import com.online.fund.spider.entity.SharesIndustry;
import com.online.fund.spider.entity.SharesTransaction;
import com.online.fund.util.HttpClientUtil;

/**
 * 获取股票行业信息
 * @author galen.zhang
 *
 */
@Service
public class SharesIndustryPageProcess {

	private final static String SHARES_TRANSACTION_PAGE_URL_SH = "http://emweb.securities.eastmoney.com/PC_HSF10/CompanySurvey/CompanySurveyAjax?code=sh#{sharesCode}.html";
	private final static String SHARES_TRANSACTION_PAGE_URL_SZ = "http://emweb.securities.eastmoney.com/PC_HSF10/CompanySurvey/CompanySurveyAjax?code=sz#{sharesCode}.html";
	
	private final static Logger logger = LoggerFactory.getLogger(SharesIndustryPageProcess.class);
	
	@Resource
	private FundDao fundDao;
	
	public void parsePage() {
		try {
			List<SharesTransaction> sharesTransactionList = null;
			
			// 上证A股
//			sharesTransactionList = fundDao.querySharesTransactionByTypeCode("C.2");
//			System.out.println(sharesTransactionList.size());
//			parsePage(sharesTransactionList, "C.2", true);

			// 上证B股
//			sharesTransactionList = fundDao.querySharesTransactionByTypeCode("C.3");
//			System.out.println(sharesTransactionList.size());
//			parsePage(sharesTransactionList, "C.3", true);

//			// 深证A股
//			sharesTransactionList = fundDao.querySharesTransactionByTypeCode("C._SZAME");
//			System.out.println(sharesTransactionList.size());
//			parsePage(sharesTransactionList, "C._SZAME", true);

//			// 深证B股
			sharesTransactionList = fundDao.querySharesTransactionByTypeCode("C.7");
			System.out.println(sharesTransactionList.size());
			parsePage(sharesTransactionList, "C.7", true);

			HttpClientUtil.closeConnectionPool();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
	
	
	public void parsePage(List<SharesTransaction> sharesTransactionList, String sharesTypeCode, boolean saveFlag) {

		try {
			List<SharesIndustry> sharesIndustryList = new ArrayList<SharesIndustry>();
			for (SharesTransaction sharesTransaction : sharesTransactionList) {
				String jsStr = null;
				if ("C.2".equals(sharesTypeCode) || "C.3".equals(sharesTypeCode)) {
					jsStr = HttpClientUtil.httpGetRequest(SHARES_TRANSACTION_PAGE_URL_SH.replace("#{sharesCode}", sharesTransaction.getSharesCode()));
				} else {
					jsStr = HttpClientUtil.httpGetRequest(SHARES_TRANSACTION_PAGE_URL_SZ.replace("#{sharesCode}", sharesTransaction.getSharesCode()));
				}
//				System.out.println(jsStr);
				JSONObject obj = JSON.parseObject(jsStr);
				JSONObject jbzlObj = (JSONObject) ((JSONObject)obj.get("Result")).get("jbzl");
				
				if (saveFlag) {
					SharesIndustry eastmoneyEntity = new SharesIndustry();
					SharesIndustry sfcEntity = new SharesIndustry();
					
					// 东方财富行业分类
					eastmoneyEntity.setSharesCode(sharesTransaction.getSharesCode());
					eastmoneyEntity.setSharesName(sharesTransaction.getSharesName());
					eastmoneyEntity.setIndustryDefineType("EASTMONEY");
					eastmoneyEntity.setIndustryName((String)jbzlObj.get("sshy"));
					
					// 证监会分类
					sfcEntity.setSharesCode(sharesTransaction.getSharesCode());
					sfcEntity.setSharesName(sharesTransaction.getSharesName());
					sfcEntity.setIndustryDefineType("SFC");
					sfcEntity.setIndustryName((String)jbzlObj.get("sszjhhy"));
					
					sharesIndustryList.add(eastmoneyEntity);
					sharesIndustryList.add(sfcEntity);
					
					if (sharesIndustryList.size() >= 100) {
						fundDao.batchAddSharesIndustry(sharesIndustryList);
						sharesIndustryList.clear();
					}
				} else {
					// 东方财富行业分类
					System.out.println(jbzlObj.get("sshy"));
					// 证监会分类
					System.out.println(jbzlObj.get("sszjhhy"));
				}
				
			}
			
			if (saveFlag && !sharesIndustryList.isEmpty()) {
				fundDao.batchAddSharesIndustry(sharesIndustryList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		SharesIndustryPageProcess processer = new SharesIndustryPageProcess();
		SharesTransaction sharesTransaction = new SharesTransaction();
		sharesTransaction.setSharesCode("900902");
		List<SharesTransaction> sharesTransactionList = new ArrayList<SharesTransaction>();
		sharesTransactionList.add(sharesTransaction);
		processer.parsePage(sharesTransactionList, "C.3", false);

		HttpClientUtil.closeConnectionPool();
	}
}
